
package edu.ncist.wang.util;


import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/*import org.junit.Test;
*/
public class JDBCUtils {
	
	
		
	
	
	//鍙厤缃細 
	
	private static String drvier ;
	private static String url ;
	private static String username ;
	private static String password ;
	
	static{
		//浣跨敤闈欐�佸揩銆傦紝 璇诲彇澶栭儴鐨勯厤缃枃浠讹細鐒跺悗缁欐垚鍛樺彉閲忚祴鍊硷細 
		
		InputStream inputStream = JDBCUtils.class.getClassLoader().getResourceAsStream("db.properties");
		
		Properties p = new Properties(); 
		try {
			p.load(inputStream);
			
			drvier= p.getProperty("drvier");
			url = p.getProperty("url");
			username = p.getProperty("username");
			password =p.getProperty("password");
		
			//娉ㄥ唽椹卞姩锛�  娉ㄥ唽椹卞姩鍙渶瑕佹敞鍐屼竴娆★細 鏀惧湪static 鍚堥�傦細 
			Class.forName(drvier);
			
			
			System.out.println(url);
		} catch (Exception e) {
			e.printStackTrace();
		} 
	 
	}
	
	
	//鑾峰緱閾炬帴瀵硅薄锛� 
	public static Connection getConnection() {
		try {
			Connection connection = DriverManager.getConnection(url, username, password);
			return connection; 
		} catch (SQLException e) {
		    throw new RuntimeException(e);
		}
	}
	
	//鍏抽棴璧勬簮灏佽锛�  
	public static void release(Connection connection, Statement stmt, ResultSet rs){
		if(connection != null){
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			//蹇�熼噴鏀捐祫婧愶細
			connection= null; 
		}
		
		if(stmt != null){
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			//蹇�熼噴鏀捐祫婧愶細
			stmt= null; 
		}
		
		if(rs != null){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			//蹇�熼噴鏀捐祫婧愶細
			rs= null; 
		}
	} 

}	
